package com.bestcem.xm.om.grpc.service;

import com.bestcem.xm.common.core.domain.web.RpcStatusResult;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.common.core.enums.ErrorCodeEnum;
import com.bestcem.xm.om.constant.CommonConstant;
import com.bestcem.xm.om.grpc.service.converter.OmPlatformConverter;
import com.bestcem.xm.om.grpc.service.param.OmPlatformParam;
import com.bestcem.xm.om.grpc.v1.services.*;
import com.bestcem.xm.om.service.PlatformService;
import com.bestcem.xm.om.service.dto.PlatformDTO;
import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.server.service.GrpcService;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 查询统计数据
 *
 * @author zhangfeng <feng.zhang@idiaoyan.com>
 * @version v1.0
 * @date 2022/1/11 11:11
 */
@Slf4j
@GrpcService
public class OmPlatformGrpcService extends PlatformServiceGrpc.PlatformServiceImplBase {
    /**
     * 机构统计服务
     */
    @Resource
    private PlatformService platformService;

    @Override
    public void getPlatformByCode(GetPlatformByCodeRequest request,
                                  StreamObserver<GetPlatformByCodeResponse> responseObserver) {
        GetPlatformByCodeResponse.Builder builder = GetPlatformByCodeResponse.newBuilder();

        String code = request.getCode();
        // 参数校验
        if (StringUtils.isEmpty(code)) {
            String errMsg = String.format(CommonConstant.ERROR_MSG_FORMATTER, ErrorCodeEnum.PARAM_ERROR.getMsg(), "code不能为空");
            builder.setStatus(RpcStatusResult.error(errMsg));
            responseObserver.onNext(builder.build());
            responseObserver.onCompleted();
            return;
        }

        // 获取统计数据
        ServiceResult<PlatformDTO> serviceResult = platformService.findOneByCode(code);

        // 返回数据
        if (serviceResult.isSuccess()) {
            Platform platform = OmPlatformConverter.dtoToResponse(serviceResult.getData());
            builder.setPlatform(platform);
            builder.setStatus(RpcStatusResult.success());
        } else {
            String errMsg = String.format(CommonConstant.ERROR_MSG_FORMATTER, ErrorCodeEnum.SYSTEM_ERROR.getMsg(), serviceResult.getMsg());
            builder.setStatus(RpcStatusResult.error(errMsg));
        }
        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }

    @Override
    public void listPlatforms(ListPlatformsRequest request,
                              StreamObserver<ListPlatformsResponse> responseObserver) {
        ListPlatformsResponse.Builder builder = ListPlatformsResponse.newBuilder();

        OmPlatformParam param = OmPlatformConverter.convertToOmPlatform(request);
        // 获取统计数据
        ServiceResult<List<PlatformDTO>> serviceResult = platformService.listPlatforms(param);

        // 返回数据
        if (serviceResult.isSuccess()) {
            List<Platform> list = serviceResult.getData().stream().map(OmPlatformConverter::dtoToResponse).collect(Collectors.toList());
            builder.addAllPlatforms(list);
            builder.setStatus(RpcStatusResult.success());
        } else {
            String errMsg = String.format(CommonConstant.ERROR_MSG_FORMATTER, ErrorCodeEnum.SYSTEM_ERROR.getMsg(), serviceResult.getMsg());
            builder.setStatus(RpcStatusResult.error(errMsg));
        }
        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }

    @Override
    public void getPlatformByOrgId(GetPlatformByOrgIdRequest request,
                                   StreamObserver<GetPlatformByOrgIdResponse> responseObserver) {
        ServiceResult<PlatformDTO> serviceResult = platformService.getPlatformByOrgId(request.getOrgId());
        GetPlatformByOrgIdResponse.Builder builder = GetPlatformByOrgIdResponse.newBuilder();
        // 返回数据
        if (serviceResult.isSuccess()) {
            builder.setPlatform(OmPlatformConverter.dtoToResponse(serviceResult.getData()));
            builder.setStatus(RpcStatusResult.success());
        } else {
            String errMsg = String.format(CommonConstant.ERROR_MSG_FORMATTER, ErrorCodeEnum.SYSTEM_ERROR.getMsg(), serviceResult.getMsg());
            builder.setStatus(RpcStatusResult.error(errMsg));
        }
        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }
}
